//
//  Labs_@autoclosure_DemoVC.swift
//  APSwift
//
//  Created by jianbo.pei on 2023/10/25.
//

/**
 ## 来源：[如何在 Swift 中使用 @autoclosure 来提高性能](https://zhuanlan.zhihu.com/p/659183634)
 */

import UIKit

class Labs__autoclosure_DemoVC: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        test1()
        test2()
        test3()
    }
    
    func test1() {
        let p = Person(name: "jianbo.pei")
        log1(p.logName)
    }
    
    struct Person {
        let name: String
        var logName: String {
            debugPrint("调用了logName属性getter")
            return "Person‘s name is \(name)"
        }
    }
    
    func log1(_ message: String) {
        if false {
            debugPrint("message is \(message)")
        }
    }
    
    func test2() {
        let p = Person(name: "jianbo.pei")
        log2 {
            p.logName
        }
    }
    
    func log2(_ message: () -> String) {
        if false {
            debugPrint("message is \(message())")
        }
    }
    
    func test3() {
        let p = Person(name: "jianbo.pei")
        log3(p.logName)
        log3(p.logName, isTrue: true)
    }
    
    func log3(_ message: @autoclosure () -> String, isTrue: Bool = false) {
        if isTrue {
            debugPrint("message is \(message())")
        }
    }

}
